<template>
	<view class="jkangkepu">
		<view class="jkangkepuback">
			<up-swiper height="265" autoplay circular="true" :list="list5" @change="e => current = e.current"
				@click="clickloop">
				<template #indicator>
					<view class="indicator">
						<view class="indicator__dot" v-for="(item, index) in list5" :key="index"
							:class="[index === current && 'indicator__dot--active']">
						</view>
					</view>
				</template>
			</up-swiper>
		</view>
		<view class="headertop" :style="{'background':back1}">
			<view class="box"
				:style="{'margin-top':statusBarHeight+'px','width':'100%','height':barHeight+'px','display':'flex','align-items': 'center'}">
				<!-- 	<image :src="_static('/images/LOGO@2x.png')"  class="pagelefticon"
					mode=""></image> -->
			</view>
		</view>
		<view class="bottombox">
			<view class="scrrowyx">
				<view :class="active==-1?'fenleibox1':'fenleibox'" @click="qiehuan(-1)">
					全部
				</view>
				<view :class="active==index?'fenleibox1':'fenleibox'" v-for="(item,index) in fenleiarr"
					@click="qiehuan(index,item)" :key="item.id">
					{{item.name}}
				</view>
			</view>
			<view class="classify">
				<view class="msgicon" v-if="!list.length">
					<image :src="_static('/images/wxnone.png')" mode="widthFix">
					</image>
					<view class="msgtext">
						暂无内容
					</view>
				</view>
				<view class="itembox" v-for="item in list" :key="item.id" @click="gopage(item)">
					<image :src="item.image" class="imgh" mode=""></image>
					<view class="textb">
						<view class="center1">
							<view class="t1">
								{{item.title}}
							</view>
							<view class="t2">
								{{item.describe}}
							</view>
							<view class="t2">
								{{formattedDate(item.createtime*1000)}}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="" style="height: 200rpx;">

		</view>
		<Tabbar page="/pages/jiankang/index"></Tabbar>
	</view>
</template>

<script setup>
	import Tabbar from '@/components/tabbar.vue'
	import {
		getCurrentInstance,
		onMounted,
		ref,
		reactive
	} from 'vue'
	import {
		$api
	} from '../../ulm';
	import {
		onLoad,
		onShow,
		onPageScroll,
		onReachBottom
	} from "@dcloudio/uni-app";
	import {
		_static
	} from '@/ulm/utils/url.js'
	const barHeight = ref('')
	const total = ref(0)
	const current = ref(0)
	const statusBarHeight = ref('')
	const query = reactive({
		page: 1,
		list_rows: 10,
		category_id: 'all'
	})
	const fenleiarr = ref([])
	const bannerContent = ref({})
	const back1 = ref('')
	const list = ref([])
	const list5 = ref([

	]);
	const active = ref(-1)
	onMounted(() => {
		statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
		const {
			top,
			height
		} = wx.getMenuButtonBoundingClientRect();
		barHeight.value = height ? height + (top - statusBarHeight.value) * 2 : 38;
	})

	onShow(() => {
		list.value = []
		query.page = 1
		fenleiinit()
		bannerlistinit()
	})

	function clickloop(e) {
		return
		if (bannerContent.value[e]['jump_page'] == 'shop') {
			uni.switchTab({
				url: '/pages/shoppingmal/shangcheng'
			})
		} else if (bannerContent.value[e]['jump_page'] == 'rent') {
			uni.switchTab({
				url: '/pages/shoppingmal/index'
			})
		}
	}
	async function bannerlistinit() {
		const res = await $api.bannerlist({
			page: 'jkkp'
		})
		if (res.code == 1) {
			list5.value = res.data.map(item => item.image)
			bannerContent.value = res.data
		}
	}
	onReachBottom(() => {
		if (total.value > list.value.length) {
			query.page++
			categorylist()
		}
	})
	const formattedDate = (timestamp) => {
		const date = new Date(timestamp)
		const year = date.getFullYear()
		const month = padStart(date.getMonth() + 1)
		const day = padStart(date.getDate())
		return `${year}-${month}-${day}`
	}

	function qiehuan(ind, item) {
		active.value = ind
		if (ind == -1) {
			query.category_id = 'all'
		} else {
			query.category_id = item.id
		}
		list.value = []
		query.page = 1
		categorylist()
	}

	function gopage(item) {
		uni.navigateTo({
			url: '/sub_jiankang/jiankang/detail?id=' + item.id
		})
	}

	function padStart(n) {
		return n.toString().padStart(2, 0)
	}
	async function fenleiinit() {
		const res = await $api.newscategory()
		if (res.code == 1) {
			fenleiarr.value = res.data
			categorylist()
		}
	}
	async function categorylist() {
		const res = await $api.category(query)
		if (res.code == 1) {
			total.value = res.data.total
			list.value.push(...res.data.data)
		}

	}
	onPageScroll((e) => {
		if (e.scrollTop > 100) {
			back1.value = '#CAEEFF'
		} else {
			back1.value = ''
		}
	})
</script>

<style lang="scss">
	::v-deep .u-swiper__indicator {
		bottom: 36px !important;
	}

	.indicator {
		@include flex(row);
		justify-content: center;

		&__dot {
			width: 30rpx;
			height: 4rpx;
			border-radius: 2rpx 2rpx 2rpx 2rpx;
			background: rgba(166, 109, 51, 0.2);
			margin: 0 2px;
			transition: background-color 0.3s;

			&--active {
				background-color: #fff;
			}
		}
	}

	.indicator-num {
		padding: 2px 0;
		background-color: rgba(0, 0, 0, 0.35);
		border-radius: 100px;
		width: 35px;
		@include flex;
		justify-content: center;

		&__text {
			color: #FFFFFF;
			font-size: 12px;
		}
	}

	.msgicon {
		width: 608rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #86909C;
			text-align: center;
			width: 100%;
			margin-top: 30rpx;
		}

		image {
			width: 608rpx;
		}
	}

	page {
		background: #F7F8FA;
	}

	.jkangkepu {
		width: 100%;
		overflow: hidden;

		.jkangkepuback {
			width: 100%;
			height: 530rpx;
			// position: absolute;
			// position: absolute;

		}

		.headertop {
			width: 100%;
			position: fixed;
			top: 0%;
			overflow: hidden;
			transition: 0.5s;
			z-index: 9999;
		}

		.pagelefticon {
			width: 120rpx;
			height: 40rpx;
			z-index: 9999;
			margin-left: 30rpx;

		}

		.bottombox {
			width: 100%;
			background: #F7F8FA;
			box-shadow: 0rpx -4rpx 8rpx 0rpx rgba(171, 227, 254, 0.31);
			border-radius: 48rpx 48rpx 0rpx 0rpx;
			overflow: hidden;
			margin-top: -60rpx;
			z-index: 999;
			position: relative;

			.classify {
				width: 686rpx;
				margin: auto;
				margin-top: 40rpx;
				display: flex;
				flex-wrap: wrap;
				align-items: center;

				.itembox:nth-child(2n) {
					margin-right: 0rpx;
				}

				.itembox {
					width: 328rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					border-radius: 24rpx;
					overflow: hidden;
					margin-bottom: 28rpx;
					margin-right: 30rpx;

					.imgh {
						width: 328rpx;
						height: 252rpx;
					}

					.textb {
						width: 328rpx;
						height: 144rpx;
						background: #FFFFFF;
						box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.07);
						overflow: hidden;

						.center1 {
							width: 286rpx;
							margin: auto;
							margin-top: 12rpx;

							.t1 {
								width: 95%;
								font-weight: 500;
								font-size: 28rpx;
								color: #1D2129;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}

							.t2 {
								margin-top: 12rpx;
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 26rpx;
								color: #86909C;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}
						}
					}
				}
			}

			.scrrowyx {
				width: 100%;
				margin-top: 32rpx;
				white-space: nowrap;
				overflow-x: scroll;
				margin-left: 32rpx;

				.fenleibox {
					min-width: 180rpx;
					height: 64rpx;
					background: #FFFFFF;
					display: inline-block;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					text-align: center;
					line-height: 64rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #4E5969;
					margin-right: 28rpx;
					padding: 0 10rpx;
				}

				.fenleibox1 {
					padding: 0 10rpx;
					margin-right: 28rpx;
					min-width: 180rpx;
					height: 64rpx;
					background: #2AB4F5;
					display: inline-block;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					text-align: center;
					line-height: 64rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
				}
			}
		}
	}
</style>